package classmate

import (
	"class2-4/shujuku"
	"crypto/md5"
	"fmt"
	"os"
	"time"

	"github.com/howeyc/gopass"
)

type Classmate struct {
	ID   int
	Name string
	Sn   string
	Pswd string
}

func Classmates() {
	var classmates []Classmate
	shujuku.DB.Raw("SELECT*FROM classmates.classmates WHERE id<5").Scan(&classmates)
	for _, xuesheng := range classmates {
		fmt.Println(xuesheng.ID, xuesheng.Name)
	}
	fmt.Println("查询完了")
}

var LoginUser Classmate

func Login() bool {
	var name string
	fmt.Println("请输入用户名")
	fmt.Scanln(&name)
	var pswd string
	fmt.Println("请输入密码")
	pswDBytes, err := gopass.GetPasswdPrompt("密码", true, os.Stdin, os.Stdout)
	if err != nil {
		fmt.Println("GetPasswdPrompt 失败，", err.Error())
		return false
	}
	pswd = string(pswDBytes)
	pswd = fmt.Sprintf("%x", md5.Sum([]byte(pswd)))
	//fmt.Scanln(&pswd)

	shujuku.DB.Raw("SELECT*FROM classmates.classmates where name=?", name).Scan(&LoginUser)
	if LoginUser.Pswd == pswd {
		fmt.Println("登录成功")
		AutoWriteLog(LoginUser.Name, LoginUser.ID, time.Now(), "登录成功", LoginUser.Sn)
		return true
	} else {
		fmt.Println("登录失败")
		return false
	}

}

func AutoWriteLog(s1 string, i int, time time.Time, s2, s3 string) {
	panic("unimplemented")
}
